package org.jeecg.modules.relation.relachstandarddistribution.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.relation.relachstandarddistribution.entity.RelAchStandardDistribution;
import org.jeecg.modules.relation.relachstandarddistribution.service.IRelAchStandardDistributionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.relation.relachstandarddistribution.vo.RelAchStandardDistributionVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: rel_ach_standard_distribution
 * @author: baochangrong
 * @Date:   2022-03-17
 * @Version: V1.0
 */
@Api(tags="rel_ach_standard_distribution")
@RestController
@RequestMapping("/relachstandarddistribution/relAchStandardDistribution")
@Slf4j
public class RelAchStandardDistributionController extends JeecgController<RelAchStandardDistribution, IRelAchStandardDistributionService> {
	@Autowired
	private IRelAchStandardDistributionService relAchStandardDistributionService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param relAchStandardDistribution
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-分页列表查询")
	@ApiOperation(value="rel_ach_standard_distribution-分页列表查询", notes="rel_ach_standard_distribution-分页列表查询")
	@GetMapping(value = "/queryList")
	public Result<?> queryPageList(RelAchStandardDistribution relAchStandardDistribution,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RelAchStandardDistribution> queryWrapper = QueryGenerator.initQueryWrapper(relAchStandardDistribution, req.getParameterMap());
		Page<RelAchStandardDistribution> page = new Page<RelAchStandardDistribution>(pageNo, pageSize);
		IPage<RelAchStandardDistribution> pageList = relAchStandardDistributionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/** voList 全部查询 分页 */
	@AutoLog(value = "voList-分页列表查询")
	@ApiOperation(value="voList-分页列表查询", notes="voList-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> voList(@Param("relAchStandardDistributionVo") RelAchStandardDistributionVo relAchStandardDistributionVo,
							@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							HttpServletRequest req) {
		//获取当前登录用户信息
		String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
		String username = JwtUtil.getUsername(token);
		LoginUser loginUser = sysBaseAPI.getUserByName(username); //.getUserById() 获取登录用户

		String tenantId = req.getHeader("tenant-id");//获取当前登录用户的租户id
		String sysOrgCode = loginUser.getOrgCode();//获取当前用户的当前的部门编码（不是id是编码）
		int userIdentity = loginUser.getUserIdentity();//获取当前登录用户身份

		relAchStandardDistributionVo.setTenantId(tenantId);
//		relAchStandardDistributionVo.setSysOrgCode(sysOrgCode);
		Page<RelAchStandardDistributionVo> page = new Page<RelAchStandardDistributionVo>(pageNo, pageSize);
		IPage<RelAchStandardDistributionVo> pageList = relAchStandardDistributionService.voList(page, relAchStandardDistributionVo);
		return Result.OK(pageList);
	}



	/**
	 *   添加
	 *
	 * @param relAchStandardDistribution
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-添加")
	@ApiOperation(value="rel_ach_standard_distribution-添加", notes="rel_ach_standard_distribution-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody RelAchStandardDistribution relAchStandardDistribution,HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		relAchStandardDistribution.setTenantId(tenantId);
		relAchStandardDistributionService.save(relAchStandardDistribution);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param relAchStandardDistribution
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-编辑")
	@ApiOperation(value="rel_ach_standard_distribution-编辑", notes="rel_ach_standard_distribution-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody RelAchStandardDistribution relAchStandardDistribution) {
		relAchStandardDistributionService.updateById(relAchStandardDistribution);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-通过id删除")
	@ApiOperation(value="rel_ach_standard_distribution-通过id删除", notes="rel_ach_standard_distribution-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		relAchStandardDistributionService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-批量删除")
	@ApiOperation(value="rel_ach_standard_distribution-批量删除", notes="rel_ach_standard_distribution-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.relAchStandardDistributionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "rel_ach_standard_distribution-通过id查询")
	@ApiOperation(value="rel_ach_standard_distribution-通过id查询", notes="rel_ach_standard_distribution-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		RelAchStandardDistribution relAchStandardDistribution = relAchStandardDistributionService.getById(id);
		if(relAchStandardDistribution==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(relAchStandardDistribution);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param relAchStandardDistribution
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, RelAchStandardDistribution relAchStandardDistribution) {
		return super.exportXls(request, relAchStandardDistribution, RelAchStandardDistribution.class, "rel_ach_standard_distribution");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, RelAchStandardDistribution.class);
	}

	/**
	 * 根据试卷ID查询 对应的试卷-标准记录
	 * */
	@AutoLog(value = "getByPaperId-试卷-标准记录")
	@ApiOperation(value="getByPaperId-试卷-标准记录", notes="getByPaperId-试卷-标准记录")
	@GetMapping(value = "/getByPaperId")
	public Result<?> queryPageList(RelAchStandardDistributionVo relAchStandardDistributionVo,
								   HttpServletRequest req) {
		String tenantId = req.getHeader("tenant-id");
		relAchStandardDistributionVo.setTenantId(tenantId);
		List<RelAchStandardDistributionVo> achStandardDistributionList = relAchStandardDistributionService.getByPaperId(relAchStandardDistributionVo);
		return Result.OK(achStandardDistributionList);
	}

}
